package com.ttg.common.utils;

public class CommUtils {
    private static final String patternString="^[a-zA-Z\\d-/._]+$";
    private static final String patternString1="./|/.";

    public static String filePathFilterClean(String str){
        return cleanString(filePathFilter(str));
    }
    public static String filePathFilter(String filepath){
        final String[] split=patternString1.split("\\|");
        for (String s:split){
            filepath=filepath.replace(s,"");
        }
        if (filepath.matches(patternString)){
            return filepath;
        }else {
            return null;
        }
    }
    public static String cleanString(String aString){
        if (aString==null)return null;
        String cleanString="";
        for (int i = 0; i < aString.length(); i++) {
            cleanString+=cleanChar(aString.charAt(i));
        }
        return cleanString;
    }
    private static char cleanChar(char aChar){
        //0-9
        for (int i=45;i<58;i++){
            if (aChar==i)return (char)i;
        }
        //A-Z
        for (int i = 65; i <91 ; i++) {
            if (aChar==i)return (char)i;
        }
        //a-z

        for (int i = 97; i <123 ; i++) {
            if (aChar==i)return (char)i;
        }
        switch (aChar){
            case '/':
                aChar='/';
                break;
            case '.':
                aChar='.';
                break;
            case '-':
                aChar='-';
                break;
            case '_':
                aChar='_';
                break;
            case ' ':
                aChar=' ';
                break;
            default:
                return '%';
        }
        return aChar;
    }
}
